package com.ssm.controller;

import com.ssm.entity.Customer;
import com.ssm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "/findone")
    @ResponseBody
    public Customer findCustomer(@RequestParam(defaultValue = "-1") int c_id) {
        Customer customer;
        if (c_id >= 0) {
            customer = customerService.findCustomerById(c_id);
            if (customer != null) {
                return customer;
            } else {
                customer = new Customer();
                customer.setC_id(-1);
                return customer;
            }
        } else {
            customer = new Customer();
            customer.setC_id(-1);
            return customer;
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public List<Customer> update(){
        return customerService.updateCustomerById();
    }
    @RequestMapping("/insert")
    public void insertCustomer(HttpServletResponse response, HttpServletRequest request) throws IOException {
        PrintWriter out = response.getWriter();
        Customer c;
        long starTimen =System.currentTimeMillis();
        int j=0;
        String name = request.getParameter("name");
        for(int i=1;i<=1000;i++){
            j=(j+1)%4;
            c = getCustomer(j);
            c.setC_name(name);
            out.println(customerService.insertCustomer(c));
        }
        long endTime = System.currentTimeMillis();
        out.println("使用的时间："+(float)(endTime-starTimen)/1000+"s!"+name);

    }

    public Customer getCustomer(int i){
        Customer c=new Customer();
        int j = new Random().nextInt(100);
        switch (i){
            case 0:
                c.setC_name("堺雅人"+j);
                c.setC_sex("男");
                c.setC_address("日本");
                c.setC_position("战士");
                break;
            case 1:
                c.setC_name("长泽雅美"+j);
                c.setC_sex("女");
                c.setC_address("大阪");
                c.setC_position("演员");
                break;
            case 2:
                c.setC_name("奥黛丽.赫本"+j);
                c.setC_sex("女");
                c.setC_address("美国");
                c.setC_position("演员");
                break;
            case 3:
                c.setC_name("刘亦菲"+j);
                c.setC_sex("女");
                c.setC_address("中国");
                c.setC_position("演员");
                break;
            default:
                c.setC_name("蛇道人"+j);
                c.setC_sex("男");
                c.setC_address("九州");
                c.setC_position("魔道");
        }
        c.setC_phone(getPhone());
        return c;
    }
    public String getPhone(){
        Random r = new Random();
        String phone ="1324226";
        for(int i=0;i<4;i++){
            phone =phone+r.nextInt(10);
        }
        return phone;
    }

}
